package cn.com.controller;

import cn.com.dao.StudentDao;
import cn.com.entity.ClassTimetable;
import cn.com.entity.Scores;
import cn.com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Project:Academic - StuController
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/14 08:49:25
 * @since 1.8
 */
public class StuController {

    private StudentDao studentDao = new StudentDao();

    public void doHome_one(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/stu/update.jsp").forward(request,response);
    }

    public void modifyNow(HttpServletRequest request,HttpServletResponse response) throws ServletException , IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        User oleId = (User) request.getSession().getAttribute("loginState");
        if (studentDao.modifyNow(name,pwd,oleId.getId())) {
            if (!name.equals(oleId.getName()))oleId.setName(name);
            if (!pwd.equals(oleId.getPwd()))oleId.setPwd(pwd);
            request.getSession().setAttribute("loginState",oleId);
            request.setAttribute("update","<span style=\"color: red\">(修改成功)</span>");
            request.getRequestDispatcher("/index.do").forward(request,response);
        }else {
            request.setAttribute("update","<span style=\"color: red\">(数据异常)</span>");
            request.getRequestDispatcher("/index.do").forward(request,response);
        }

    }
    public void doHome_two(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException{
        User user = (User) request.getSession().getAttribute("loginState");
        List<ClassTimetable> list = studentDao.find(user.getClazz());
        request.setAttribute("clazz",list);
        request.getRequestDispatcher("/WEB-INF/jsp/stu/show.jsp").forward(request,response);
    }

    public void doHome_three(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException{
        User user = (User) request.getSession().getAttribute("loginState");

        List<Scores> list = studentDao.findScores(user.getId());
        System.out.println(list);
        request.setAttribute("score",list);
        request.getRequestDispatcher("/WEB-INF/jsp/stu/find.jsp").forward(request,response);

    }
}
